package com.bjpowernode.controller;

import com.bjpowernode.service.ThumbsService;
import com.bjpowernode.user.Dynamic;
import com.bjpowernode.user.Thumbs;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping(value = "thumbs")
public class ThumbsController {

    @Resource
    private ThumbsService thumbsService;

    @RequestMapping(value = "/add.do",produces = "application/text;charset=utf-8")
    @ResponseBody
    public String addThumbs(@RequestParam("username") String username,@RequestParam("dzid") Integer dzid){
        Thumbs thumbs = new Thumbs();
        thumbs.setUsername(username);
        thumbs.setDzid(dzid);
        int res = thumbsService.addThumbs(thumbs);
        if (res == 0){
            int result = thumbsService.removeThumbs(thumbs);
            return result == 1 ? "已取消点赞" : "取消点赞失败！";
        }
        return "已点赞";
    }

    @RequestMapping(value = "/selectSelf.do")
    public ModelAndView selectSelf(String name){
        ModelAndView mv = new ModelAndView();
        List<Dynamic> thumbsList = thumbsService.findSelfThumbs(name);
        mv.addObject("thumbsList",thumbsList);
        mv.setViewName("userThumbs");
        return mv;
    }
}
